#import "sprite_library_treasure_exhibition_dialog.h"

#if (HARD_WARE == HARD_WARE_IPHONE)
  #define TREASURE_POEM_FONT_SIZE 12
#elif (HARD_WARE == HARD_WARE_IPAD)
  #define TREASURE_POEM_FONT_SIZE 24
#endif

@implementation sprite_library_treasure_exhibition_dialog

- (id)initLibraryTreasureExhibition:(int)Id
{
	CCSprite *_Image;

	if ((self = [super init]))
	{
		flagDrag_ = false;
		flagCanTextDrag_ = false;
		switch (Id)
		{
		case 0 :
		case 1 :
		case 2 :
		case 3 :
		case 4 :
		case 5 :
		case 6 :
		case 7 :
			type_ = 0;
			strcpy(voice, g_CfgExhibitionElementTable[Id].path);
			_Image = [CCSprite spriteWithSpriteFrameName:TREASURE_ICON_MUSIC_PATH];
			[self addChild:_Image];
			playVoiceRect_.center = CGPointZero;
			playVoiceRect_.size = _Image.contentSizeInPixels;
			break;

		case 8 :
		case 9 :
		case 10 :
		case 11 :
			type_ = 1;
			[self Load:g_CfgExhibitionElementTable[Id].path];
			break;

		case 12 :
			type_ = 2;
			_Image = [CCSprite spriteWithFile:@"Artworks_1.png"];
			[self addChild:_Image];
			break;

		case 13 :
			type_ = 2;
			_Image = [CCSprite spriteWithFile:@"Artworks_2.png"];
			[self addChild:_Image];
			break;

		case 14 :
			type_ = 2;
			_Image = [CCSprite spriteWithFile:@"Artworks_3.png"];
			[self addChild:_Image];
			break;

		case 15 :
			type_ = 2;
			_Image = [CCSprite spriteWithFile:@"Artworks_4.png"];
			[self addChild:_Image];
			break;

		default :;
		}
		[self ActionOpen];
	}
	return self;
}

- (void)draw
{
	UICenterRect _Rect;

	_Rect.center = CGPointZero;
	_Rect.size = g_ScrrenSize;
	glColor4f(0.0f, 0.0f, 0.0f, 0.8f);
	uiRenderRect(_Rect);
}

- (void)Load:(char const *)FileName
{
	int i;
	int _Index;
	float _Y;
	float _Height;
	NSString *_Path;
	NSString *_String;
	NSArray *_Array;
	CCLabelTTF *_Label;

	_Path = [CCFileUtils fullPathFromRelativePath:[NSString stringWithUTF8String:FileName]];
	_String = [NSString stringWithContentsOfFile:_Path encoding:NSUTF8StringEncoding error:nil];
	_Array = [_String componentsSeparatedByString:@"\n"];
	_Index = 0;
	for (NSString *string in _Array)
	{
		_Label = [CCLabelTTF labelWithString:string fontName:TREASURE_UTF16_FONT_NAME fontSize:TREASURE_POEM_FONT_SIZE];
		[self addChild:_Label];
		_Label.anchorPoint = ccp(0.5f, 1.0f);
		textTable_.push_back(_Label);
	}
	_Height = textTable_[0].contentSizeInPixels.height * textTable_.size() + (textTable_.size() - 1) * TREASURE_EXHIBITION_TEXT_LINE_OFFSET;
	if (_Height < g_ScrrenSize.height)
	{
		flagCanTextDrag_ = false;
		_Y = _Height / 2.0f;
	}
	else
	{
		flagCanTextDrag_ = true;
		_Y = g_ScrrenSize.height / 2.0f;
	}
	for (i = 0; i < textTable_.size(); i++)
	{
		textTable_[i].positionInPixels = ccp(0, _Y);
		_Y -= textTable_[i].contentSizeInPixels.height + TREASURE_EXHIBITION_TEXT_LINE_OFFSET;
	}
	textTable_.back(). anchorPoint = ccp(0.0f, 1.0f);
}

- (void)TouchBegan:(CGPoint)Point
{
	flagDrag_ = false;
	start_ = Point;
	end_ = Point;
}

- (void)TouchMoved:(CGPoint)Point
{
	int i;
	float _Offset;
	CGPoint _Temp;

	flagDrag_ = true;
	if ((1 == type_) && flagCanTextDrag_)
	{
		_Temp = Point;
		_Offset = _Temp.y - end_.y;
		if (_Offset < 0.0f)
		{
			if (textTable_[0].positionInPixels.y <= g_ScrrenSize.height / 2.0f)
			{
				return;
			}
			if (textTable_[0].positionInPixels.y + _Offset < g_ScrrenSize.height / 2.0f)
			{
				_Offset = g_ScrrenSize.height / 2.0f - textTable_[0].positionInPixels.y;
			}
		}
		else if (_Offset > 0.0f)
		{
			if (textTable_.back().positionInPixels.y >= textTable_[0].contentSizeInPixels.height)
			{
				return;
			}
			if (textTable_.back().positionInPixels.y + _Offset > textTable_[0].contentSizeInPixels.height)
			{
				_Offset = textTable_[0].contentSizeInPixels.height - textTable_.back().positionInPixels.y;
			}
		}
		else
		{
			return;
		}
		for (i = 0; i < textTable_.size(); i++)
		{
			textTable_[i].positionInPixels = ccp(0, textTable_[i].positionInPixels.y + _Offset);
		}
	}
	end_ = Point;
}

- (void)TouchEnded:(CGPoint)Point
{
	end_ = Point;
	if (!flagDrag_)
	{
		/*voice
		 */
		if (0 == type_)
		{
			if (uiPointInCenterRect(ccpSub(end_, self.positionInPixels), playVoiceRect_))
			{
				EffectPlay(voice);
				return;
			}
		}

		/*effect
		 */
		EffectButton();

		/*close
		 */
		[self ActionClose];
	}
}

- (void)ActionOpen
{
	CCSequence *_Seq;
	id _ActionOpen;
	CCCallFuncN *_CallBack;

	self.scale = 0.0f;
	_ActionOpen = [CCScaleTo actionWithDuration:TREASURE_EXHIBITION_OPEN_TIME scaleX:1.0f scaleY:1.0f];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionOpenOver:)];
	_Seq = [CCSequence actions:_ActionOpen, _CallBack, nil];
	[self runAction:_Seq];

	g_LayerLibraryTreasure.flagTouchLock = true;
}

- (void)ActionClose
{
	CCSequence *_Seq;
	id _ActionClose;
	CCCallFuncN *_CallBack;

	self.scale = 1.0f;
	_ActionClose = [CCScaleTo actionWithDuration:TREASURE_EXHIBITION_CLOSE_TIME scaleX:0.0f scaleY:0.0f];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionCloseOver:)];
	_Seq = [CCSequence actions:_ActionClose, _CallBack, nil];
	[self runAction:_Seq];

	g_LayerLibraryTreasure.flagTouchLock = true;
}

- (void)CallBackActionOpenOver:(id)Sender
{
	if (0 == type_)
	{
		EffectPlay(voice);
	}
	g_LayerLibraryTreasure.flagTouchLock = false;
}

- (void)CallBackActionCloseOver:(id)Sender
{
	g_LayerLibraryTreasure.flagTouchLock = false;
	[g_LayerLibraryTreasure DestroyExhibitonView];
}

@end